using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using DataBase;
using DataBase.DBmodels;
using DataBase.Utils;
using GreenXi.Model;
using Microsoft.EntityFrameworkCore;
using Prism.Commands;
using static PressSystem_Can.Help.Can.CanHelper;

namespace GreenXi.ViewModel
{
    public class PeakValleyTimeViewModel: PeakValleyTimeModel
    {
        private Pages.Navig.PeakValleyTimeEdit peakValleyEdit;
        public PeakValleyTimeViewModel()
        {

            using MyDbContext myDbContext = new();

            TimeslotList= new(GlobalVariables.TimeslotList);

            SportsMode = new ObservableCollection<string>
            {
                "峰段",
                "谷段",

            };

            SaveColumn = new DelegateCommand<object>((p) => { SaveColumn_Click(p); });//保存栏目
            CloseWindows = new DelegateCommand<object>((p) => { CloseWindows_Click(); });//关闭编辑窗口

            DelProject = new DelegateCommand<object>((p) => { DelProject_Click(p); });//删除项目
            AddProject = new DelegateCommand(() => { AddProject_Click(); });          //新增项目
            EditProject = new DelegateCommand<object>((p) => { EditProject_Click(p); });//编辑项目
            SaveProject = new DelegateCommand<object>((p) => { SaveProject_Click(p); });//保存项目

        }




        #region 项目
        private void EditProject_Click(Object param)
        {
            var paramss = param as timeslot;
            Timeslot = paramss;
            peakValleyEdit = new();
            peakValleyEdit.DataContext = this;
            peakValleyEdit.ShowDialog();
        }


        private void SaveProject_Click(Object param)
        {
            using MyDbContext myDbContext = new();
            myDbContext.UpdateTimeslot(Timeslot);
        }
        private async void AddProject_Click()
        {
            var region = new timeslot
            {
                Type = "峰段",
                ISEnable = true,
            };
            TimeslotList.Add(region);

            using MyDbContext myDbContext = new();
            await myDbContext.AddAsync<DataBase.DBmodels.timeslot>(region);

            init();

        }
        private async void DelProject_Click(object param)//删除项目
        {
            var paramss = param as timeslot;
            using MyDbContext myDbContext = new();
            await myDbContext.DeleteAsync<timeslot>(paramss);
            TimeslotList.Remove(paramss);

            init();
        }
        #endregion

        private void SaveColumn_Click(Object param)
        {
            using MyDbContext myDbContext = new();
            myDbContext.UpdateTimeslot(Timeslot);
            MessageBox.Show("修改成功！");
            peakValleyEdit.Close();//关闭弹窗

            init();
        }
        private void CloseWindows_Click()
        {
            peakValleyEdit.Close();//关闭弹窗
        }
        public void init()
        {
            using MyDbContext myDbContext = new();
            var list = myDbContext.GetTimeslot();
            GlobalVariables.TimeslotList = list;
            TimeslotList = new(list);
        }
    }
}
